//
// Created by lenovo on 2024/7/2.
//
#include "./GameObj.h"
#include "../Msg/Message.h"
/**
 * @note
 * 由于图片与collider没有视觉差距
 * 故collider的实际pos不需要添加offset
 * 直接collider.setPosition(p)即可
 */

namespace Game::Obj{
    Bullet::Bullet() {
        shape = new sf::CircleShape();
        shape->setRadius(10);
        shape->setFillColor(sf::Color::Blue);

        position = Vector2f ({300,300});
        scale = Vector2f ({20,20});
        shape->setRadius(scale.x/2);
        shape->setPosition(
                position
                );
        collider = new CircleCollider ;
        collider->setPosition(
                position
                +
                Vector2f({scale.x / 2 , scale.y / 2})
                );
//        collider->setPosition(position);
        collider->setRadius(scale.x / 2 );

//        CollisionManager::getInstance().registe(this);
    }

    void Bullet::update(float deltaTime) {
        shape->setPosition(
                position
        );
        collider->setPosition(
                position
                +
                Vector2f({scale.x / 2 , scale.y / 2})
        );
//        collider->setPosition(position);
    }

    void Bullet::draw(sf::RenderWindow &window) {
        if (shape)
            window.draw(*shape);
//        if (collider)
//            drawCollider(window , *collider );
    }

    void Bullet::setPosition(const sf::Vector2f &newPosition)  {
        position = newPosition;
        shape->setPosition(newPosition);
        collider->setPosition(
                position
                +
                Vector2f({scale.x / 2 , scale.y / 2})
        );
//        collider->setPosition(position);
    }

    void Bullet::onCollision(Game::Obj::GameObj *other) {
        //do-nothing
    }

    void Bullet::destroy() {
        using namespace Game::Msg ;

        MsgBus::getInstance().post(
                new DestroyMsg(this)
        );
        // 删除碰撞体
        cout << "send destroy msg" << endl ;

    }
}